﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace QuickDev.SSO
{
    /// <summary>
    /// ResponseToken
    /// </summary>
    [Serializable]
    public class ResponseToken
    {
        #region Private Fields
        private int _userId;
        private string _userName;
        private string _roles;
        private DateTime _timeStamp;
        private int _expireDuration;
        private string _seed;
        private int _resultCode;
        #endregion

        #region Public Properties

        /// <summary>
        /// UserId
        /// </summary>
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        /// <summary>
        /// UserName
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        /// <summary>
        /// Roles
        /// </summary>
        public string Roles
        {
            get { return _roles; }
            set { _roles = value; }
        }

        /// <summary>
        /// TimeStamp
        /// </summary>
        public DateTime TimeStamp
        {
            get { return _timeStamp; }
            set { _timeStamp = value; }
        }

        /// <summary>
        /// ExpireDuration
        /// </summary>
        public int ExpireDuration
        {
            get { return _expireDuration; }
            set { _expireDuration = value; }
        }

        /// <summary>
        /// Seed
        /// </summary>
        public string Seed
        {
            get { return _seed; }
            set { _seed = value; }
        }

        /// <summary>
        /// ResultCode
        /// </summary>
        public int ResultCode
        {
            get { return _resultCode; }
            set { _resultCode = value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// ResponseToken
        /// </summary>
        /// <param name="userId">userId</param>
        /// <param name="userName">userName</param>
        /// <param name="roles">roles</param>
        /// <param name="timeStamp">timeStamp</param>
        /// <param name="expireDuration">expireDuration</param>
        /// <param name="seed">seed</param>
        /// <param name="resultCode">resultCode</param>
        public ResponseToken( int userId , string userName , string roles , DateTime timeStamp , int expireDuration , string seed , int resultCode )
        {
            _userId = userId;
            _userName = userName;
            _roles = roles;
            _timeStamp = timeStamp;
            _expireDuration = expireDuration;
            _seed = seed;
            _resultCode = resultCode;
        }

        /// <summary>
        /// Encode
        /// </summary>
        /// <returns>HttpUtility.UrlEncode( encodedText )</returns>
        public string Encode()
        {
            string encodedText = SsoCipher.EncryptVector
                ( _userId.ToString() ,
                _userName ,
                _roles ,
                _timeStamp == default( DateTime ) ? null : _timeStamp.ToString() ,
                _expireDuration.ToString() ,
                _seed ,
                _resultCode.ToString() );
            return HttpUtility.UrlEncode( encodedText );
        }

        /// <summary>
        /// ToString
        /// </summary>
        /// <returns>Encode()</returns>
        public override string ToString()
        {
            return Encode();
        }

        /// <summary>
        /// TryParse
        /// </summary>
        /// <param name="tokenText">tokenText</param>
        /// <param name="token">token</param>
        /// <returns>bool successful</returns>
        public static bool TryParse( string tokenText , out ResponseToken token )
        {
            token = null;
            if ( string.IsNullOrEmpty( tokenText ) )
                return false;
            string textToDecypt = HttpUtility.UrlDecode( tokenText );
            string[] vector = null;
            if ( !SsoCipher.TryParseVector( textToDecypt , out vector ) )
                return false;
            if ( vector.Length != 7 )
                return false;

            int userId = Convert.ToInt32( vector[0] );
            string userName = vector[1];
            string roles = vector[2];
            DateTime timeStamp = Convert.ToDateTime( vector[3] );
            int expire = Convert.ToInt32( vector[4] );
            string seed = vector[5];
            int resultCode = Convert.ToInt32( vector[6] );
            token = new ResponseToken( userId , userName , roles , timeStamp , expire , seed , resultCode );
            return true;
        }

        #endregion
    }
}